package de.bensoft.jrail.client.ui.settings;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.stage.Stage;

import javax.inject.Inject;

import jfxtras.labs.scene.control.BeanPathAdapter;
import de.bensoft.jrail.business.entity.Setting;
import de.bensoft.jrail.business.service.SettingsService;
import de.bensoft.jrail.client.com.CommunicationHelper;
import de.bensoft.jrail.client.common.JavaFXController;

@JavaFXController(fxml="Settings.fxml")
public class SettingsController {
	
	// **************************************************************************************
	// STATICS
	// **************************************************************************************
	private static final String[] baudrates = new String[] { "9600" };
	
	// **************************************************************************************
	// EXT
	// **************************************************************************************
	@Inject
	SettingsService settingsService;
		
	@Inject
	CommunicationHelper commHelper;
	
	// **************************************************************************************
	// FXML
	// **************************************************************************************
	@FXML
	private ComboBox<String> cboxPort, cboxBaud;	
	@FXML
	private void onOk(ActionEvent ae) {	
		save();
		
		Button b = (Button)ae.getSource();
		Stage s = (Stage)b.getScene().getWindow();
		s.close();
	}	
	@FXML
	private void onCancel(ActionEvent ae) {
		Button b = (Button)ae.getSource();
		Stage s = (Stage)b.getScene().getWindow();
		s.close();
	}
	
	@FXML
	private void initialize() {
		initGUI();
	}
	// **************************************************************************************
	// GUI
	// **************************************************************************************
	Setting stgPort, stgBaud;
	private void initGUI() {
		
		// Ports
		ObservableList<String> lstports = FXCollections.observableArrayList(commHelper.getAvailablePorts());
		cboxPort.setItems(lstports);
		stgPort = settingsService.findSettingByIdentifier(SettingsService.SETTING_PORT);
		BeanPathAdapter<Setting> bpaPort = new BeanPathAdapter<Setting>(stgPort);
		bpaPort.bindBidirectional("stringValue", cboxPort.valueProperty(), String.class);

		
		// Bauds
		ObservableList<String> lstbauds = FXCollections.observableArrayList(baudrates);
		cboxBaud.setItems(lstbauds);
		stgBaud = settingsService.findSettingByIdentifier(SettingsService.SETTING_BAUD);
		BeanPathAdapter<Setting> bpaBaud = new BeanPathAdapter<Setting>(stgBaud);
		bpaBaud.bindBidirectional("longValue", cboxBaud.valueProperty(), String.class);
		
	}
	
	// **************************************************************************************
	// DATA
	// **************************************************************************************
	
	private void save() {
		settingsService.saveSetting(stgPort);
		settingsService.saveSetting(stgBaud);
	}

}
